package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dingtao.common.bean.CheckInfo;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.CheckInPresenter;
import com.example.peibei.service.presenter.CheckInfoPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CheckInActivity extends WDActivity {
    @BindView(R.id.tv_days)
    TextView tv_days;
    @BindView(R.id.tv_check_number)
    TextView tv_check_number;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.rg_check_in)
    RadioGroup rg_check_in;
    @BindView(R.id.bt_check_in)
    Button bt_check_in;

    private SPUtils spUtils;
    private String token;
    private CheckInfoPresenter checkInfoPresenter;
    private CheckInPresenter checkInPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_in;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        checkInPresenter = new CheckInPresenter(new CheckInCall());
        checkInfoPresenter = new CheckInfoPresenter(new CheckInfoCall());
        checkInfoPresenter.reqeust("Bearer " + token);
        for (int i = 0; i < rg_check_in.getChildCount(); i++) {
            rg_check_in.getChildAt(i).setEnabled(false);
        }
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.bt_check_in)
    public void checkIn() {
        checkInPresenter.reqeust("Bearer " + token);
    }

    class CheckInfoCall implements DataCall<CheckInfo> {

        @Override
        public void success(CheckInfo data, Object... args) {
            tv_days.setText(data.getContinuous() + "");
            if (data.isTodaySignIn()) {
                bt_check_in.setText("已签到");
                bt_check_in.setClickable(false);
            } else {
                bt_check_in.setText("签到");
                bt_check_in.setClickable(true);
            }
            List<CheckInfo.WeeksDTO> weeks = data.getWeeks();
            for (int i = 0; i < weeks.size(); i++) {
                CheckInfo.WeeksDTO weeksDTO = weeks.get(i);
                int week = weeksDTO.getWeek();
                RadioButton radioButton = (RadioButton) rg_check_in.getChildAt(week - 1);
                if (weeksDTO.getSignIn() == 0) {
                    radioButton.setChecked(false);
                } else {
                    radioButton.setChecked(true);
                }
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class CheckInCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("签到成功");
            checkInfoPresenter.reqeust("Bearer " + token);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @Override
    protected void destoryData() {

    }
}
